import { _decorator, Component, instantiate, Node, RichText, SpriteAtlas, Tween, tween, UITransform, v3 } from 'cc';
import Ab_Config_bA from '../../manager/config.manager';
import Ab_GameDC_bA from '../../manager/data.game.center';
import Ab_GameAssets_bA from '../../manager/game.assets';
//import { RichTextCheck } from '../../../A-FRAME/component/richtext.check';
const { ccclass, property } = _decorator;

@ccclass('Ab_LivecommentBlock_bA')
export class Ab_LivecommentBlock_bA extends Component {
	@property(UITransform)
	private view: UITransform = null;
	@property(SpriteAtlas)
	private atlas_channel: SpriteAtlas = null;

	private Ab_item_demo_bA: Node;
	private Ab_curr_token_bA: string;

	private Ab_prms_inited_bA: Promise<'end'>;

	protected onLoad(){
		this.Ab_init_bA();
	}

	protected onEnable(){
		this.Ab_prms_inited_bA.then(()=>this.Ab_refresh_bA());
	}

	private Ab_init_bA(){
		this.Ab_item_demo_bA = this.view.node.children[0];
		this.view.node.removeAllChildren();
		this.Ab_initRender_bA();
	}

	private Ab_initRender_bA(){
		let item_height = this.Ab_item_demo_bA.getComponent(UITransform).height;

		let count = 2;

		let list = Ab_GameDC_bA.Ab_getLiveComments_bA(count);

		this.Ab_prms_inited_bA = list.Ab_forWait_bA(async (data, i)=>{
			let node = instantiate(this.Ab_item_demo_bA);
			node.setParent(this.view.node);
			node.setPosition(0, -i * item_height, node.position.z);

			await this.Ab_renderItem_bA(node, data);
		});
	}

	private Ab_renderItem_bA(node: Node, data: ReturnType<typeof Ab_GameDC_bA.Ab_getLiveComments_bA>[number]){
		node.name = [data.nickname, data.count, data.symbol].join('-');
		let richtext = node.getChildByName('richtext').getComponent(RichText);
		richtext.string = Ab_GameAssets_bA.Ab_getLangText_bA(10,
			'<color=#FFF583>' + data.nickname + '</color>',
			'<color=#FF5A5A>' + data.count + '</color>',
			'<color=#5EF769>' + Ab_Config_bA.Ab_symbolString_bA(data.symbol) + '</color>',
		);
		abi.Ab_cc_bA.Ab_skinPeeler_bA(node.getChildByName('icon'), this.atlas_channel.getSpriteFrame(data.channel), {width: true});

		//return richtext.getComponent(RichTextCheck).forceAdatptFontSize(true);
		richtext.node.active = true;
	}

	private Ab_refresh_bA(){
		if(this.Ab_curr_token_bA) this.Ab_curr_token_bA = abi.Ab_string_bA.Ab_randomToken_bA(3, 36, t=>t!=this.Ab_curr_token_bA);
		else this.Ab_curr_token_bA = abi.Ab_string_bA.Ab_random_bA(3, 36);

		this.Ab_showNext_bA();
	}

	private async Ab_showNext_bA(){
		let token = this.Ab_curr_token_bA;
		let [curr_item, next_item] = [...this.view.node.children].sort((a, b)=>b.position.y - a.position.y);
		let list = Ab_GameDC_bA.Ab_getLiveComments_bA();
		let next_index = list.findIndex(el=>[el.nickname, el.count, el.symbol].join('-')==curr_item.name) + 1;
		next_index = next_index % list.length;
		await this.Ab_renderItem_bA(next_item, list[next_index]);

		let duration = 1;

		let [tw_curr, tw_next] = [curr_item, next_item].map((node, i)=>{
			Tween.stopAllByTarget(node);
			let ui = node.getComponent(UITransform);
			return tween(node)
			.to(duration, {position: v3(node.position.x, (1-i) * ui.height, node.position.z)})
		});

		tw_next.start();
		tw_curr.call(()=>{
			let ui = curr_item.getComponent(UITransform);
			curr_item.setPosition(curr_item.position.x, -ui.height, curr_item.position.z);
			this.scheduleOnce(()=>this.Ab_curr_token_bA==token ? this.Ab_showNext_bA() : void 0, 1.5);
		}).start();
	}
}

